import { storeManager } from "../store";
import { addHandler } from "./handlers.ts";
import { logger, LogCategory } from '../logger';

/**
 * 设置存储相关的IPC处理程序
 */
export function setupStoreIpcHandlers() {
  logger.info('StoreHandler', LogCategory.IPC, '设置存储IPC处理程序');

  // 设置相关
  addHandler('get-settings', () => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, '获取应用设置');
      return storeManager.settings.getSettings();
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '获取应用设置失败', error);
      throw error;
    }
  });

  addHandler('update-settings', (_, settings) => {
    try {
      logger.info('StoreHandler', LogCategory.IPC, '更新应用设置', settings);
      storeManager.settings.updateSettings(settings);
      return true;
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '更新应用设置失败', error);
      throw error;
    }
  });

  // 书签相关
  addHandler('get-bookmarks', () => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, '获取书签数据');
      return storeManager.bookmarks.getBookmarks();
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '获取书签数据失败', error);
      throw error;
    }
  });

  addHandler('add-bookmark', (_, bookmark) => {
    try {
      logger.info('StoreHandler', LogCategory.IPC, `添加书签: ${bookmark.title}, URL: ${bookmark.url}`);
      return storeManager.bookmarks.addBookmark(bookmark);
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '添加书签失败', error);
      throw error;
    }
  });
  
  addHandler('delete-bookmark', (_, id) => {
    try {
      logger.info('StoreHandler', LogCategory.IPC, `删除书签: ${id}`);
      return storeManager.bookmarks.deleteBookmark(id);
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '删除书签失败', error);
      throw error;
    }
  });

  // 历史记录相关
  addHandler('get-history', () => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, '获取历史记录');
      return storeManager.history.getHistory();
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '获取历史记录失败', error);
      throw error;
    }
  });

  addHandler('add-history-entry', (_, entry) => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, `添加历史记录: ${entry.title}, URL: ${entry.url}`);
      storeManager.history.addHistoryEntry(entry);
      return true;
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '添加历史记录失败', error);
      throw error;
    }
  });
  
  addHandler('clear-history', (_, { startTime, endTime }) => {
    try {
      if (startTime && endTime) {
        logger.info('StoreHandler', LogCategory.IPC, `清除指定时间段历史记录`);
        storeManager.history.clearHistory(startTime, endTime);
      } else {
        logger.info('StoreHandler', LogCategory.IPC, '清除所有历史记录');
        storeManager.history.clearHistory();
      }
      return true;
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '清除历史记录失败', error);
      throw error;
    }
  });

  // 会话相关
  addHandler('get-current-tabs', () => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, '获取当前会话标签页');
      return storeManager.session.getCurrentTabs();
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '获取当前会话标签页失败', error);
      throw error;
    }
  });

  addHandler('update-current-tabs', (_, tabs) => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, `更新当前会话标签页，数量: ${tabs.length}`);
      storeManager.session.updateCurrentTabs(tabs);
      return true;
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '更新当前会话标签页失败', error);
      throw error;
    }
  });

  addHandler('get-last-closed-tabs', () => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, '获取最近关闭的标签页');
      return storeManager.session.getLastClosedTabs();
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '获取最近关闭的标签页失败', error);
      throw error;
    }
  });

  addHandler('add-to-last-closed-tabs', (_, tab) => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, `添加到最近关闭的标签页: ${tab.title}, URL: ${tab.url}`);
      storeManager.session.addToLastClosedTabs(tab);
      return true;
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '添加到最近关闭的标签页失败', error);
      throw error;
    }
  });
  
  addHandler('restore-closed-tab', (_, index = 0) => {
    try {
      logger.debug('StoreHandler', LogCategory.IPC, `恢复关闭的标签页，索引: ${index}`);
      return storeManager.session.restoreClosedTab(index);
    } catch (error) {
      logger.error('StoreHandler', LogCategory.IPC, '恢复关闭的标签页失败', error);
      throw error;
    }
  });

  logger.info('StoreHandler', LogCategory.IPC, '存储IPC处理程序设置完成');
} 